package com.atguigu.gulimall.order.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.HashMap;

@Configuration
public class MyRabbitConfig {
    @Bean
    MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }
    //定制rabbitmq
//    @Autowired
//    private RabbitTemplate rabbitTemplate;
//    @PostConstruct
//    public void initRabbit(){
//        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
//            /**
//             * broker接收到消息的回调
//             * @param correlationData
//             * @param b  是否ack
//             * @param s  失败原因
//             */
//            @Override
//            public void confirm(CorrelationData correlationData, boolean b, String s) {
//
//            }
//        });
//        rabbitTemplate.setReturnCallback(new RabbitTemplate.ReturnCallback() {
//            /**
//             * exchange没有正确抵达队列后的回调
//             * @param message
//             * @param i
//             * @param s
//             * @param s1
//             * @param s2
//             */
//            @Override
//            public void returnedMessage(Message message, int i, String s, String s1, String s2) {
//
//            }
//        });
//
//    }

    @Bean
    Exchange orderEventExchange() {
        return new TopicExchange("order-event-exchange",
                true,
                false);
    }

    @Bean
    Queue orderDelayQueue() {
        HashMap<String, Object> args = new HashMap<>();
        args.put("x-dead-letter-exchange", "order-event-exchange");
        args.put("x-dead-letter-routing-key", "order.release.order");
        args.put("x-message-ttl", 5000);
        return new Queue("order.delay.queue",
                true,
                false,
                false, args);
    }

    @Bean
    Queue orderReleaseQueue() {
        return new Queue("order.release.order.queue",
                true, false, false,
                null);
    }

    @Bean
    Queue orderSeckillQueue() {
        return new Queue("order.seckill.order.queue",
                true,
                false,
                false);
    }

    @Bean
    Binding orderSeckillBinding() {
       return new Binding("order.seckill.order.queue",
                Binding.DestinationType.QUEUE,
               "order-event-exchange",
                "order.seckill.order",
                null);
    }

    @Bean
    Binding oderDelayBinding() {
        return new Binding("order.delay.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.create.order",
                null);
    }

    @Bean
    Binding oderReleaseBinding() {
        return new Binding("order.release.order.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.release.order",
                null);
    }

}
